/**
 * Created by liuzengrong on 2017/9/17.
 */
import Constants from './Constants';
import { message } from 'antd';

export function formatMobile(mobile) {
  var reg = /^[0-9]{11,20}$/;
  if (reg.exec(mobile)) {
    return mobile.substring(0, 3) + '-' + mobile.substring(3, 7) + '-' + mobile.substring(7, 11);
  }
  return mobile;
}

export function download(url, conditions) {
  let conditionsStr = '';
  let _url = url;
  for (let p in conditions) {
    conditionsStr += p + '=' + conditions[p] + '&';
  }
  conditionsStr += Constants.ACJSESSIONID + '=' + sessionStorage.getItem(Constants.ACJSESSIONID);
  if (conditionsStr !== '') _url += '?' + conditionsStr;
  message.info('请耐心等待下载...');
  window.location.href = _url;
}

export function downloadFile(type, url) {
  if (url && type) {
    url = MC_PATH + url;
    let fileList = [];
    if (url) {
      let fileName = encodeURI(encodeURI(url.substring(url.lastIndexOf('/') + 1, url.length)));
      let fileUrl = MC_PATH + '/api/file/' + type + '/download?fileName=' + fileName;

      let dowUrl =
        fileUrl +
        '&' +
        Constants.ACJSESSIONID +
        '=' +
        sessionStorage.getItem(Constants.ACJSESSIONID);
      window.location.href = dowUrl;
    }
  }
}
